package util;

import java.math.BigDecimal;

public class UnitConversion {
	public static int psiToPLC(double value) {
		double kpa = value * 6.894757293168361D;

		BigDecimal plcValue = new BigDecimal(kpa * 8.0D);

		int result = plcValue.intValue();

		if (result < 0)
			return 0;

		return result;
	}

	public static float plcToTemperatureDegreeA6(float plcValue) {
		if (plcValue < 800.0F) {
			return 0.0F;
		}

		float celsius = (plcValue - 800.0F) * 0.0125F + 34.0F;

		return celsius;
	}

	public static float plcToTemperatureDegree(float plcValue) {
		if (plcValue < 800.0F) {
			return 0.0F;
		}

		float celsius = (plcValue - 800.0F) * 0.03125F;

		return celsius;
	}

	public static int inchesHgToPLC(double value) {
		double kpa = value * 3.386389970779419D;

		BigDecimal plcValue = new BigDecimal(kpa * 50.0D);

		int result = plcValue.intValue();

		if (result < 0)
			return 0;

		return result;
	}

	public static float plcToInchesHg(float plcValue) {
		if (plcValue < 800.0F) {
			return 0.0F;
		}

		float kpa = (plcValue - 800.0F) / 16.0F - 100.0F;

		kpa *= -1.0F;

		float inchMercury = kpa * 0.2953F;

		return inchMercury;
	}

	public static float plcToPsi(float plcValue) {
		if (plcValue < 800.0F) {
			return 0.0F;
		}

		float kpa = (plcValue - 800.0F) / 6.4F;

		float psi = kpa * 0.1450377F;

		return psi;
	}
	
	
	public static double convertMicronsToInchesHg(double value) {
		BigDecimal micron = new BigDecimal(value);

		BigDecimal micron2 = micron.setScale(1, 1);

		double micron3 = micron2.doubleValue();

		micron3 *= 2.0D;

		BigDecimal micron4 = new BigDecimal(micron3);

		BigDecimal micron5 = micron4.setScale(0, 1);

		return micron5.doubleValue();
	}
	
	public static double convertInchesHgToMicrons(double value) {
		BigDecimal micron = new BigDecimal(value);

		BigDecimal micron2 = micron.setScale(1, 1);

		double micron3 = micron2.doubleValue();

		micron3 /= 2.0D;

		BigDecimal micron4 = new BigDecimal(micron3);

		BigDecimal micron5 = micron4.setScale(0, 1);

		return micron5.doubleValue();
	}
	
	//Data send in Hex in high byte and low byte order
	 public static String readeableToPlcHex(int value)
	   {
	    String hexValue = Integer.toHexString(value);

	    while (hexValue.length() < 4)
	    {
     hexValue = "0" + hexValue;
    }
	    String highByte =  hexValue.substring(2, 4);
	    String lowByte = hexValue.substring(0, 2);
	    String plcValue = highByte + lowByte;

	     return plcValue.toUpperCase();
	   }

	 //Hex receive from PLC is high byte , low byte order , need to change it to low byte and high byte order
	  public static int plcHexToReadeable(String plcHex)
	   {
	    StringBuffer sb = new StringBuffer();
	    CharSequence highByte =  plcHex.subSequence(0, 2);
	    CharSequence lowByte = plcHex.subSequence(2, 4);
	    sb.append(lowByte);
	    sb.append(highByte);
	
	    int i = Integer.valueOf(sb.toString(), 16).intValue();
	
	   return i;
	   }

	public static void main(String[] args) {
//		System.out.println(inchesHgToPLC(13.0D));
//
//		System.out.println(psiToPLC(50.0D));
//		System.out.println(plcToPsi(801.0F));
		System.out.println(convertMicronsToInchesHg(20.1234567));
	}
}
